#pragma once 

#include <iostream>
#include <string>
#include <sstream>
#include <sys/stat.h>
#include "Util.hpp"

#define HOMEPAGE "index.html"

const std::string sep = "\r\n";
const std::string default_path = "./wwwroot";

class request
{
public:
    void parse()
    {
        std::string line = Util::getOneline(inbuffer, sep);
        std::stringstream ss(line);
        ss >> method >> url >> version;

        path = default_path;
        path += url;
        if(path[path.size()-1] == '/') path += HOMEPAGE;

        auto n = path.rfind(".");
        if(n == std::string::npos) suffix = "html";
        else
        {
            suffix = path.substr(n);
        }

        struct stat st;
        if(!stat(path.c_str(), &st)) size = -1;
        else size = st.st_size;
    }
public:
    std::string inbuffer;
    std::string headline;
    std::string requestHeader;
    std::string blankline;
    std::string body;

    std::string path;
    std::string url;
    std::string method;//get post
    std::string version;//HTTP/1.1
    std::string suffix;
    int size;
};

class response
{
public:

public:
    std::string outbuffer;
};